package db_live

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_live"
)

func GetLiveCategory(param map[string]interface{}) (*models_live.LiveCategory, error) {
	fName := "GetLiveCategory"
	o := &models_live.LiveCategory{}
	table := global.Live().Model(&models_live.LiveCategory{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(&o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
func GetLiveCategoryParam(param map[string]interface{}, order string) ([]*models_live.LiveCategory, error) {
	fName := "GetLiveCategoryParam"
	o := make([]*models_live.LiveCategory, 0)
	table := global.Live().Model(&models_live.LiveCategory{})
	mysql.Condition(table, param, "", order, 0, 0)
	err := table.Find(&o).Order("sort").Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetLiveCategoryTopParam(param map[string]interface{}, order string) ([]*models_live.LiveCategoryTop, error) {
	fName := "GetLiveCategoryTopParam"
	o := make([]*models_live.LiveCategoryTop, 0)
	table := global.Live().Model(&models_live.LiveCategoryTop{})
	mysql.Condition(table, param, "", order, 0, 0)
	err := table.Find(&o).Order("sort").Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
